// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: BasicApplicationCreator.java 51 2007-04-08 12:37:12Z alexandre.roman $

package fast4j.ace.impl.basic;

import java.io.File;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.io.FileUtils;

import fast4j.ace.ApplicationDescriptor;
import fast4j.ace.PropertyDescriptor;
import fast4j.ace.impl.AbstractGroovyApplicationCreator;

/**
 * Basic application creator. This class generates a simple application, with
 * the famous "Hello world". This creator is used by default.
 * <p>
 * Supported properties:
 * </p>
 * <ul>
 * <li><strong>ui</strong>: user interface (<code>swing</code> or
 * <code>text</code>)</li>
 * <li><strong>packager</strong>: packaging tool (<code>ant</code> or
 * <code>maven2</code>)</li>
 * </ul>
 * @author alexandre.roman
 */
public class BasicApplicationCreator extends AbstractGroovyApplicationCreator {
    private static final ResourceBundle RESOURCES = ResourceBundle
            .getBundle(BasicApplicationCreator.class.getPackage().getName()
                    + ".Resources");
    
    public BasicApplicationCreator() {
        super();
        
        // fill supported properties
        supportedProperties.add(new PropertyDescriptor("ui", RESOURCES
                .getString("basic.property.ui"), "text"));
        supportedProperties.add(new PropertyDescriptor("packager", RESOURCES
                .getString("basic.property.packager"), "maven2"));
        
    }
    
    @Override
    protected void doCreate(ApplicationDescriptor desc, File dir)
            throws Exception {
        final Map<String, String> bindings = createBindings(desc);
        bindings.put("id", desc.getId());
        bindings.put("name", desc.getName());
        bindings.put("version", desc.getVersion());
        bindings.put("rootPackage", desc.getRootPackage());
        
        log.debug("Application properties: {}", bindings);
        
        final String packager = bindings.get("packager");
        
        final File baseDir = new File(dir, desc.getId());
        if ("maven2".equalsIgnoreCase(packager)) {
            final File pomFile = new File(baseDir, "pom.xml");
            log.info("Creating pom.xml");
            writeToFile(pomFile, "pom.xml.template", bindings);
        } else if ("ant".equalsIgnoreCase(packager)) {
            final File buildFile = new File(baseDir, "build.xml");
            log.info("Creating build.xml");
            writeToFile(buildFile, "build.xml.template", bindings);
        } else {
            throw new IllegalArgumentException(
                    "Unsupported property value for packager: " + packager);
        }
        
        final File srcMainJavaDir = new File(baseDir, "src/main/java");
        final File mainFile = new File(srcMainJavaDir, desc.getRootPackage()
                .replace('.', '/')
                + "/Main.java");
        log.info("Creating Main.java");
        writeToFile(mainFile, "Main.java.template", bindings);
        
        final File srcMainResourcesDir = new File(baseDir, "src/main/resources");
        FileUtils.forceMkdir(srcMainResourcesDir);
    }
    
    /**
     * Return <code>"basic"</code>.
     */
    public String getId() {
        return "basic";
    }
    
    public String getDescription() {
        return RESOURCES.getString("basic.description");
    }
    
    public String getName() {
        return RESOURCES.getString("basic");
    }
}
